package JAVA300.第十二章网络编程;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * tcp
 * 模拟登录--双向输入登录信息
 *
 * 添加分析传入数据
 */

public class web05_TCP5_MultiLoginServer {
    public static void main(String[] args) throws IOException {
        System.out.println("--------Server-----------");
        //1。指定端口 使用ServerSocket创建服务器
        ServerSocket server = new ServerSocket(8888);
        boolean isRunning = true;

        //2.阻塞式的等待链接 accept

        while (isRunning) {
            Socket socket = server.accept();
            System.out.println("一个客户端建立了连接");
            //3。操作：输入输出流
            new Thread(new Channel(socket)).start();

        }
        server.close();

    }


    //将socket进行封装
    static class Channel implements Runnable{

        private Socket socket;
        //输入流输出流封装
        private DataInputStream dis;
        private DataOutputStream dos ;

        public Channel(Socket socket) {
            this.socket = socket;

            try {
                dis = new DataInputStream(socket.getInputStream());
                dos = new DataOutputStream(socket.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
                //出现异常
               release();
            }
        }

        @Override
        public void run() {
            // System.out.println(msg);
            //*******************************分析数据

            String username = "";
            String userpwd = "";

            String[] datas = receive().split("&");
            for (String info : datas) {
                String[] usrinfo = info.split("=");

                if (usrinfo[0].equals("uname")) {
                    username = usrinfo[1];
                } else if (usrinfo[0].equals("upwd")) {
                    userpwd = usrinfo[1];
                }

            }
            //登录成功

            if (username.equals("admin") && userpwd.equals("admin")) {
                // System.out.println("登录成功！！");
                //输出流返回客户端
                send("登录成功！！");

            } else {
               send("登录失败！！");
            }

            //4.释放资源
            release();

        }

        //1。---接收数据用的
        private String receive(){
            String data = "";
            try {
                data = dis.readUTF();

            } catch (IOException e) {
                e.printStackTrace();
            }
            return data;
        }

        //2。---发送数据用的
        private void send(String msg){
            try {
                dos.writeUTF(msg);
                dos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //3.释放资源
        private void release(){
            try {
                if (null != dis){
                    dis.close();
                }
                if (null != dos){
                    dos.close();
                }
                if (null != socket){
                    socket.close();

                }


            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

}
